package ren.xbcr.schoolbus.client.custom.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import ren.xbcr.schoolbus.client.R;
import ren.xbcr.schoolbus.client.domain.BusDriver;
import ren.xbcr.schoolbus.client.fragment.RatingDriverFragment;
import ren.xbcr.schoolbus.client.util.PrefUtils;

/**
 * Created by Jian on 2015/12/5.
 */
public class DriverRatingListAdapter extends ArrayAdapter<BusDriver> {

    private final Activity context;
    private final BusDriver[] busDrivers;
    public DriverRatingListAdapter(Activity context, BusDriver[] busDrivers) {
        super(context, R.layout.list_driver_rating, busDrivers);

        this.context=context;
        this.busDrivers=busDrivers;
    }

    public View getView(int position,View view,ViewGroup parent) {

        BusDriver currentDriver = busDrivers[position];
        String telephone = PrefUtils.getStringFromPrefs(context, PrefUtils.PREFS_LOGIN_TELEPHONE, null);
        if (telephone==null || telephone.isEmpty()){
            return null;
        }
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.list_driver_rating, null, true);

        TextView driverName = (TextView) rowView.findViewById(R.id.driver_name);
        TextView isRating = (TextView) rowView.findViewById(R.id.is_driver_rating);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.driver_icon);
        RatingBar ratingBar = (RatingBar) rowView.findViewById(R.id.driver_rating_bar);

        if (currentDriver.getIsRating()){
            isRating.setText(this.context.getString(R.string.is_rating));
            ratingBar.setIsIndicator(true);
            ratingBar.setRating(currentDriver.getRatingNum());
        }else {
            isRating.setVisibility(View.INVISIBLE);
        }

        ratingBar.setOnRatingBarChangeListener(new RatingBarChangeListener(this.context,
                currentDriver.getId(), telephone, currentDriver.getName()));

        driverName.setText(currentDriver.getName());
        new ImageLoadTask(currentDriver.getImageUrl(), imageView).execute();
//        extratxt.setText("Description "+itemname[position]);
        return rowView;

    };

    private class RatingBarChangeListener implements RatingBar.OnRatingBarChangeListener{

        private Long mDriverId;
        private String mDriverName;
        private final Activity mContext;
        private final String mTelephoneNum;
        public RatingBarChangeListener(Activity context, Long driverId, String telephoneNum, String driverName){
            mDriverId = driverId;
            mDriverName = driverName;
            this.mContext = context;
            mTelephoneNum = telephoneNum;

        }
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

            if (!fromUser){
                return;
            }
            DialogFragment dialog = RatingDriverFragment.newInstance(mDriverId, Math.round(rating), mTelephoneNum, mDriverName, ratingBar);

            dialog.show(((FragmentActivity)this.mContext).getSupportFragmentManager(), "RatingDriverFragment");
            /*CharSequence text = ((FragmentActivity)context).getString(R.string.call_car_message);
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, busDrivers[mPosition].getName(), duration);
            toast.show();*/
        }
    }

    private class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {

        private String url;
        private ImageView imageView;

        public ImageLoadTask(String url, ImageView imageView) {
            this.url = url;
            this.imageView = imageView;
        }

        @Override
        protected Bitmap doInBackground(Void... params) {
            try {
                URL urlConnection = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) urlConnection
                        .openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                Bitmap myBitmap = BitmapFactory.decodeStream(input);
                return myBitmap;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
            imageView.setImageBitmap(result);
        }

    }
}
